<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Mail;


class ResetController extends Controller
{
    public function getReset()
    {
    	return view('home.reset.reset');
    }

    public function postAjaxemail(Request $request)
    {
    	$Receiver=$request->Receiver;
    	$res=DB::table('interviewee')->where('email',$request->Receiver)->first();
    	if(!$res)
    	{
    		return response()->json(0);
    	}else
    	{

    		$email='mail.'.trim(strrchr($request->Receiver,'@'),'@');
    		
    		// $s=Mail::send('admin.reset.email', ['user'=>$res], function ($m) use ($res)
    		$s=Mail::send('home.reset.email',['user'=>$res],function($m) use ($res)
    		{
    			$m->to($res->email)->subject('找回密码');
    		});
    		if($s)
    		{
    			return response()->json($email);
    		}
    		return response()->json(2);
    	}
    }

    public function getUpdate($token,$email)
    {
    	
    	$res=DB::table('interviewee')->where('token',$token)->first();
    	if($res)
    	{
    		return view('home.reset.update',['email'=>$email]);
    	}else
    	{
    		
    	}

    }
    public function postAjaxp(Request $request)
    {
    	$part='/[0-9a-zA-Z_]{6,18}/';
    	$res=preg_match($part,$request->Password);
    	if(!$res)
    	{
    		return response()->json(0);
    	}
    	if($request->Password!=$request->PasswordConfirm)
    	{
    		return response()->json(1);
    	}
    	$re=DB::table('interviewee')->where('email',$request->email)->update(['password'=>Crypt::encrypt($request->Password)]);
    	if($re)
    	{
    		return response()->json(2);
    	}

    }




}
